﻿using Microsoft.Extensions.DependencyInjection;
using RulesEngine.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkflowComm;



namespace WorkflowComm
{
    public class DefaultWorkflowRulesEngine : IWorkflowRulesEngine
    {
        public async Task<IEnumerable<WorkflowRuleResult>> ExecuteAsync(List<WorkflowRule> rules, params object[] data)
        {
          var rs=   rules.Select(m => new Rule
            {
                RuleName = m.RuleName,
                ErrorMessage = m.ErrorMessage,
                RuleExpressionType = RuleExpressionType.LambdaExpression
            });
            List<Workflow> workflows = new List<Workflow>()
            {
                new Workflow {      Rules = rs,     WorkflowName="工作流迁移规则"    }
            };
            var bre = new RulesEngine.RulesEngine(workflows.ToArray()  , null);
            List<RuleResultTree> resultList = await bre.ExecuteAllRulesAsync(workflows[0].WorkflowName, data);

            var result = resultList.Select(m => new WorkflowRuleResult
            {
                ExceptionMessage = m.ExceptionMessage,
                IsSuccess = m.IsSuccess,
                Rule = new WorkflowRule { RuleName = m.Rule.RuleName, Expression = m.Rule.Expression, ErrorMessage = m.Rule.ErrorMessage }
            });
            return result;
        }
    }
}
